package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.CertificateTypeContent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TypeListAdapter extends ArrayAdapter<CertificateTypeContent> {

  Map<Integer, Boolean> isCheckMap = new HashMap<Integer, Boolean>();

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2016-03-03 10:10:01 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvTypeName;
    public final CheckBox cbTypeName;

    private ViewHolder(LinearLayout rootView, TextView tvTypeName, CheckBox cbTypeName) {
      this.rootView = rootView;
      this.tvTypeName = tvTypeName;
      this.cbTypeName = cbTypeName;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvTypeName = (TextView) rootView.findViewById(R.id.tv_type_name);
      CheckBox cbTypeName = (CheckBox) rootView.findViewById(R.id.cb_type_name);
      return new ViewHolder(rootView, tvTypeName, cbTypeName);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_type_list, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    CertificateTypeContent item = getItem(position);
    //单据类别名称
    vh.tvTypeName.setText(item.getCertificateName());

    //复选框点击事件

    //		vh.cbTypeName.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    //
    //			@Override
    //			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    //				int radiaoId = Integer.parseInt(buttonView.getTag().toString());
    //
    //				//如果被选中,添加到集合
    //				if (isChecked) {
    //
    //				}
    //				//如果没选中，从集合中删除
    //				else {
    //
    //				}
    //
    //			}
    //		});

    vh.cbTypeName.setTag(position);
    // 找到需要选中的条目
    if (isCheckMap != null && isCheckMap.containsKey(position)) {
      vh.cbTypeName.setChecked(isCheckMap.get(position));
    } else {
      vh.cbTypeName.setChecked(false);
    }

    vh.cbTypeName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      private int mCheckPosition;

      @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        mCheckPosition = Integer.parseInt(buttonView.getTag().toString());
        if (isChecked) {
          // 将选中的放入hashmap中
          isCheckMap.put(mCheckPosition, isChecked);
        } else {
          // 取消选中的则剔除
          isCheckMap.remove(mCheckPosition);
        }
      }
    });

    return vh.rootView;
  }

  public boolean isCheckedItem(int position) {
    if (isCheckMap != null && isCheckMap.containsKey(position)) {
      return true;
    } else {
      return false;
    }
  }

  private LayoutInflater mInflater;

  // Constructors
  public TypeListAdapter(Context context, List<CertificateTypeContent> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public TypeListAdapter(Context context, CertificateTypeContent[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  @Override public boolean isEmpty() {
    return false;
  }
}

